package lab3;

/**
 * This class defines the Course object for the system.
 * 
 * @author A.J. Lattanze, CMU
 * @version 1.2, 2011-Feb-24
 */

/*
 * Modification Log **********************************************************
 * 
 * v1.3 Nigel Tingley, 2011-Mar-12 - Added timetable attribute to help track
 * schedule conflicts plus related methods. Condensed into "canRegisterStudent"
 * 
 * v1.2, R. Champagne, 2011-Feb-24 - Various refactorings, conversion of
 * comments to javadoc format.
 * 
 * v1.1, R. Champagne, 2002-Jun-19 - Adapted for use at ETS.
 * 
 * v1.0, A.J. Lattanze, 12/29/99 - Original version.
 * ***************************************************************************
 */

public class Course {
	/** Course title */
	private String courseName;

	/** Course number */
	private String courseID;

	/** Course section (group) */
	private String section;

	/** Days the course will be held on */
	private String days;

	/** Start time of the course */
	private String startTime;

	/** Stop time of the course */
	private String stopTime;

	/** Instructor Name */
	private String instructor;

	/** List of students registered for the course */
	private StudentList registered = new StudentList();
	
	/**
	 * 5 by 48 table of times representing 30 minute time 
	 * blocks of time - x's are filled time spaces
	 */
	private char[][] timeTable = new char[4][48];

	public Course() {
		this(null);
	}

	public Course(String courseID) {
		this.courseID = courseID;
		this.courseName = null;
		this.section = null;
		this.days = null;
		this.startTime = null;
		this.stopTime = null;
		this.instructor = null;
	}

	/**
	 * Register a student for a class.
	 * 
	 * @param studentObject
	 * @return true is registration is successful, false otherwise.
	 */
	public boolean registerStudent(Student studentObject) {

		if (registered.isStudentInList(studentObject)) {
			return (false);
		} else {
			registered.addStudent(studentObject);
			return (true);
		}
	}

	/**
	 * Register a student for a class.
	 * 
	 * @param studentObject
	 */
	public void register(Student studentObject) {
		registered.addStudent(studentObject);
	}

	/**
	 * @return The list of students registered for the course.
	 */
	public StudentList getRegistered() {
		return registered;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	public void setCourseID(String courseID) {
		this.courseID = courseID;
	}

	public void setSection(String section) {
		this.section = section;
	}

	public void setDays(String days) {
		this.days = days;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public void setStopTime(String stopTime) {
		this.stopTime = stopTime;
	}

	public void setInstructor(String instructor) {
		this.instructor = instructor;
	}

	public String getCourseName() {
		return courseName;
	}

	public String getCourseID() {
		return courseID;
	}

	public String getSection() {
		return section;
	}

	public String getDays() {
		return days;
	}

	public String getStartTime() {
		return startTime;
	}

	public String getStopTime() {
		return stopTime;
	}

	public String getInstructor() {
		return instructor;
	}
	
	public char[][] getTimeTable(){
		return timeTable;
	}
	
	/**
	 * Creates a timetable which can be compared to find out if there are conflicts.
	 */
	public void setTimeTable() {
		if (startTime != null && stopTime != null && days != null){
			int start = Integer.parseInt(startTime);
			int stop = Integer.parseInt(stopTime);
			if (start%100 == 30)
				start+=20;
			start/=50;
			if (stop%100 == 30)
				stop+=20;
			stop/=50;
			
			
			int startBackup = start;
			
			
			if (days.contains("L")){
				for (;start<stop;start++){
					timeTable[0][start] = 'x';
				}
				start = startBackup;
			}
			if (days.contains("MA")){
				for (;start<stop;start++){
					timeTable[1][start] = 'x';
				}
				start = startBackup;
			}
			if (days.contains("ME")){
				for (;start<stop;start++){
					timeTable[2][start] = 'x';
				}
				start = startBackup;
			}
			if (days.contains("J")){
				for (;start<stop;start++){
					timeTable[3][start] = 'x';
				}
				start = startBackup;
			}
			if (days.contains("V")){
				for (;start<stop;start++){
					timeTable[4][start] = 'x';
				}
			}
		}
	}
	
	/**
	 * Compares a course's timetable to another to check for schedule conflicts
	 * 
	 * @param table1
	 * @return false if there is a schedule conflict, true if there is none
	 */
	public boolean TableCompare(char[][] table1){
		int x = 0;
		int y = 0;
		
		for (;x<5;x++){
			for(;y<48;y++){
				if (table1[x][y] == 'x' && this.timeTable[x][y] == 'x')
					return false;
			}
		}
		return true; 
	}
	/**
	 * Checks if student is already registered in the Course
	 * @param StudentObject
	 * @return false if he is not already registered
	 */
	public boolean alreadyRegistered(Student StudentObject){
		
		
		CourseList StudentObjectCourseList = StudentObject.getCoursesRegistered();
		
		//checks if already registered for this very same course
		if (StudentObjectCourseList.findCourseByID(this.courseID) == this) {
			System.out.println("Already registered for this course");
			return true;	
		}
		
		return false;
	}
	/**
	 * checks if chosen course is in conflict with another course student is 
	 * registered to
	 * @param StudentObject
	 * @return false if they are not in conflict
	 */
	public boolean timeCheck (Student StudentObject){
		boolean Done = false;
		CourseList StudentObjectCourseList = StudentObject.getCoursesRegistered();
		
		StudentObjectCourseList.goToFrontOfList();
		while (!Done) {

			
			Course currentCourse = StudentObjectCourseList.getNextCourse();
			
			if (currentCourse == null) {
					
				Done = true;
				
			} else {
			
					if (!currentCourse.TableCompare(this.timeTable)){
						System.out.println("Course Time Conflict");
						return false;
					}
			}//if
		} // while
		return true;
	}
	public boolean timeCheckNoMessage (Student StudentObject){
		boolean Done = false;
		CourseList StudentObjectCourseList = StudentObject.getCoursesRegistered();
		
		StudentObjectCourseList.goToFrontOfList();
		while (!Done) {

			
			Course currentCourse = StudentObjectCourseList.getNextCourse();
			
			if (currentCourse == null) {
					
				Done = true;
				
			} else {
			
					if (!currentCourse.TableCompare(this.timeTable)){
						return false;
					}
			}//if
		} // while
		return true;
	}
	
}